package org.example.java8.optional;

import java.util.Optional;

public class Test {

	public static void main(String[] args) {
		Integer a = null;
		Integer b = 10;
		Integer c = null;
		
		// 不确定值为null,取值可用bOptional.orElse(0)
		Optional<Integer> aOptional = Optional.ofNullable(a);
		Optional<Integer> cOptional = Optional.ofNullable(c);
		
		// 确定值绝不为null,取值可用bOptional.get()
		Optional<Integer> bOptional = Optional.of(b);

		
		System.out.println(sum(aOptional, bOptional));
		System.out.println(sum(aOptional, cOptional));
	}
	
	public static int sum(Optional<Integer> optional1, Optional<Integer> optional2) {
		
		System.out.println("参数optional1是否存在：" + optional1.isPresent());
		System.out.println("参数optional2是否存在：" + optional2.isPresent());
		
		return optional1.orElse(0) + optional1.orElse(0);
	}

}
